﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Frw.Services.WindowsServices;
using Frw.Protocols.Communications.Tcp;
using Test.Services.Commands;
using Frw.Windows.Services.Monitor;

namespace Test.WindowsService
{
    public partial class Service1 : FrwServiceBase
    {
        Communicator<ServiceCommand> m_server = null;
        

        public Service1() : base("MySectionGroup")
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            this.EnableMonitor("MySectionGroup");

            m_server = new Communicator<ServiceCommand>("MySectionGroup");

            m_server.DataRecieved += new EventHandler<Frw.EventArgs<ServiceCommand>>(m_server_OnDataRecieved);
            m_server.ActAsServer(); //("127.0.0.1", 7734);

            m_server.ActAsClient(); //("127.0.0.1", 7735);
        }

        protected override void OnStop()
        {
            base.OnStop();

            if (m_server != null)
            {
                m_server.Dispose();
                m_server = null;
            }
        }

        void m_server_OnDataRecieved(object sender, Frw.EventArgs<ServiceCommand> e)
        {
            string ret = e.TargetObject.CommunicationBag.ToString();

            Logger.Log(Frw.LogLevel.Info, ret);

            m_server.Send(new ServiceCommand() { CommunicationBag = Guid.NewGuid().ToString() });
        }
    }
}
